/***************************************************************
*  Copyright (c) 2007 by GroupMe! Team (www.groupme.net)
*  All rights reserved.
*
*  This file is part of the GroupMe! Project. Source code of 
*  this project is closed and redistribution of this code is
*  prohibited. 
*  
*  Contact: http://www.groupme.net
*
*  This copyright notice MUST APPEAR in all copies of the file!
***************************************************************/
package net.groupme.utils;

import java.io.IOException;
import java.net.URL;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import net.groupme.model.MODEL_CONSTANTS;
import net.groupme.model.ObjectFactory;
import net.groupme.model.dao.DATABASE_CONSTANTS;

/**
 * created on 16.04.2007
 * 
 * This class provides some utility functions regarding resources.
 *
 * @author Mischa Frank <a href="mailto:code@planetfrank.net">code@planetfrank.net</a> 
 * @author Fabian Abel, <a href="mailto:abel@l3s.de">abel@l3s.de</a>
 * @author last edited by: $Author: fabian $
 * 
 * @version $Revision: 1.4 $ $Date: 2007-12-17 19:45:15 $
 *
 */
public class ResourceTools {
	/** Logger for this class and subclasses */
	protected final static  Log logger = LogFactory.getLog(ResourceTools.class);
	
	
	public static String getDatatypeByURL(URL url){
		try {
			return url.openConnection().getContentType();
		} catch (IOException e) {
			e.printStackTrace();
			return ("Could not find the URL: " + url.toString() + ".");
		}
	}
	
	public static int getResourceSizeInKBByURL(URL url){
		try {
			logger.info("Size: " + (url.openConnection().getContentLength()/8));
			return url.openConnection().getContentLength()/8;
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return 0;
		}
	}
	
	/**
	 * This method returns <code>true</code> if a resource with the given
	 * id exists in the underlying database.
	 * @param id the id of the Resource.
	 * @param isGroup the type of the resource has to be a group, hence: {@link MODEL_CONSTANTS#RESOURCE_TYPE_GROUP}
	 * @return  <code>true</code> if a resource with the given
	 * id exists in the underlying database, otherwise <code>false</code>.
	 */
	public static boolean doesResourceWithGivenIdExists(String id, boolean isGroup){
		Statement sqlStatement = null;
		ResultSet resource = null;
		try {
			sqlStatement = ObjectFactory.ds.getConnection().createStatement();
			resource = sqlStatement.executeQuery("SELECT * FROM " + DATABASE_CONSTANTS.TABLE_RESOURCE + " WHERE " + DATABASE_CONSTANTS.RESOURCE_ID + " = '" + id + "' AND " + DATABASE_CONSTANTS.RESOURCE_TYPE + " " + (isGroup ? ("= '" + MODEL_CONSTANTS.RESOURCE_TYPE_GROUP ) : ("!= '" + MODEL_CONSTANTS.RESOURCE_TYPE_GROUP )) + "'");
			if(resource.next()){
				return true;
			}
		} catch (SQLException e) {
			logger.error("Unable to connect to DB while querying for Tag Cloud.", e);
		}
		return false;
	}
	
	/**
	 * This method returns the id of the resource if a resource with the given
	 * URL already exists in the underlying database.
	 * @param url the url of the Resource.
	 * @return  the id of the resource if a resource with the given
	 * url already exists in the underlying database, otherwise <code>null</code>.
	 */
	public static String doesResourceWithGivenUrlExists(String id){
		Statement sqlStatement = null;
		ResultSet resource = null;
		try {
			sqlStatement = ObjectFactory.ds.getConnection().createStatement();
			resource = sqlStatement.executeQuery("SELECT " + DATABASE_CONSTANTS.RESOURCE_ID + " FROM " + DATABASE_CONSTANTS.TABLE_RESOURCE + " WHERE " + DATABASE_CONSTANTS.RESOURCE_URL + " = '" + id + "' ");
			if(resource.next()){
				return resource.getString(1);
			}
		} catch (SQLException e) {
			logger.error("Unable to connect to DB while querying for Tag Cloud.", e);
		}
		return null;
	}
}
